//
//  YDComposePictureView.swift
//  YDWeibo01
//
//  Created by suibian on 16/8/17.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit
private let composePictureViewCellId = "YDComposePictureViewCellId"
class YDComposePictureView: UICollectionView {
//    保存image的数组
    var imageList:[UIImage] = [UIImage]()
//    01声明一个闭包
    var closure: (()->())?

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

//    恭外界调用的方法
    func addImage(image:UIImage){
//        外界调用的时候就有图片了
//        显示图片
        self.hidden = false
  
//        添加到数组中
        imageList.append(image)
//        刷新UI
        reloadData()

    }
    
//    设置视图
    private func setUpUI(){
//        隐藏当前图片 
        self.hidden = true
        backgroundColor = UIColor.whiteColor()
//        注册cell
        registerClass(YDComposePictureViewCell.self, forCellWithReuseIdentifier: composePictureViewCellId)
        
//设置代理
        dataSource = self
        delegate = self
        
//      设置layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
//        设置itemsize
        let margin:CGFloat = 5
        let cellWH: CGFloat = (KSCREENW - 20 - 2 * margin)/3
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
//        设置垂直和水平间距
        layout.minimumInteritemSpacing = margin
        layout.minimumLineSpacing = margin
        
        
}

}
extension YDComposePictureView:UICollectionViewDataSource,UICollectionViewDelegate{


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        如果现实为0 或者为9 没有加号图片
        let count = imageList.count
        if count == 0 || count == 9{
        return count
        
        }
        return imageList.count + 1
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(composePictureViewCellId, forIndexPath: indexPath) as! YDComposePictureViewCell
//        给外界赋值
        if imageList.count == indexPath.item{
            cell.image = nil
        }else{
            cell.image = imageList[indexPath.item]
//            实例化闭包
            cell.closure = {[weak self] in
//            删除制定图片
                self?.imageList.removeAtIndex(indexPath.item)
//                若没有图片了
                if self?.imageList.count == 0{
//                隐藏显示
                self?.hidden = true 
                }
//                刷新ui
                self?.reloadData()
            }
        }
        
        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
//        如果不是0张也不是9张 最后一个是加好图片
        if imageList.count == indexPath.item{
//        03 执行闭包
        closure?()
        
        }
    }
}
// 自定义collection cell
class YDComposePictureViewCell:UICollectionViewCell{
//    定义一个闭包 获取数组
    var closure:(()->())?
//    供外界赋值
    var image:UIImage?{
        didSet{
//        加号图片
            if image == nil{
            bgImageView.image = UIImage(named: "compose_pic_add")
            bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                deleteButton.hidden = true
            }else{
//                解决了cell的复用问题
            bgImageView.highlightedImage = nil
            bgImageView.image = image
            deleteButton.hidden = false
            }
        
        }
    
    
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //    删除按钮的点击事件
    @objc private func buttonClick(){
        
        print("删除按钮点击")
//        03
        closure?()
        
    }
//      设置视图
    private func setUpUI(){
    
    backgroundColor = UIColor.whiteColor()
//        添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
//        添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        deleteButton.snp_makeConstraints(closure: { (make) -> Void in
            make.trailing.top.equalTo(contentView)
        })
        }
    }
//懒加载控件
    private lazy var bgImageView:UIImageView = UIImageView()
    
    //    懒加载按钮
    private lazy var deleteButton:UIButton = {
        let button = UIButton()
        //       添加点击事件
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "compose_photo_close"), forState: UIControlState.Normal)

        return button
    
    
    }()
}



















